import json
import os
import requests

# https://app9a6jfvbz4568.h5.xiaoeknow.com/
ids = ("p_6078ff28e4b071a81eb27f69",'')
cookie = 'sensorsdata2015jssdkcross=%7B%22%24device_id%22%3A%22181f0fc9d373ea-0b59f2d3082c7-74492d21-2073600-181f0fc9d38c76%22%7D; xiaoe_loading_show=1; shop_version_type=4; dataUpJssdkCookie={"wxver":"","net":"","sid":""}; h5_transport_time=2022-09-15+20%3A03%3A49; anony_token=ebdb253564f4ce77095358aa32460a05; xenbyfpfUnhLsdkZbX=0; ko_token=0ab1f38dd36a23947580e0ec422d28c3; sa_jssdk_2015_app9a6jfvbz4568_h5_xiaoeknow_com=%7B%22distinct_id%22%3A%22u_60ac6d40738fc_hfnSYkNVXB%22%2C%22first_id%22%3A%22181f0fc9d373ea-0b59f2d3082c7-74492d21-2073600-181f0fc9d38c76%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%7D; logintime=1663249451'

idx = 100
for id in ids:
    if(len(id) <= 0):
        continue
    for i in range(100):
        url = 'https://app9a6jfvbz4568.h5.xiaoeknow.com/column_more_data_v2/column_more_data_exclude_try'
        data = {
            'bizData[isDesc]': 1,
            'bizData[page_size]': i,
            'bizData[product_id]': id
        }
        headers = {
            "Cookie": cookie,
            "referer": "https://app9a6jfvbz4568.h5.xiaoeknow.com/p/course/column/p_61de833ee4b0373be5661177",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,"
                      "application/signed-exchange;v=b3;q=0.9",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/"
                          "89.0.4389.90 Safari/537.36"
        }
        res = requests.post(url, data, headers=headers)

        json_obj = json.loads(res.content)
        arr = json_obj['data']['contentData']['contentInfo']
        if (len(arr) == 0):
            break;
        for item in arr:
            resource_id = item['resource_id']
            name = item['resource_name']
            # url = 'https://app9a6jfvbz4568.h5.xiaoeknow.com/%s.m3u8' % resource_id
            url = 'https://app9a6jfvbz4568.h5.xiaoeknow.com/_alive/v2/get_lookback_url?app_id={}&alive_id={}'
            url = url.format(id,resource_id)

            res = requests.get(url, headers=headers)
            json_obj = json.loads(res.content)
            m3u8 = json_obj['data']['aliveVideoUrl']
            # print(name)
            # print(url)
            bat = 'N_m3u8DL-CLI_v3.0.2.exe "{}" --workDir "E:\\小鹅通" --saveName "{}" --enableDelAfterDone'
            idx = idx+1
            print(bat.format(m3u8,str(idx)+"_"+name))
